<?php

/**
 * AppServer.
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-07 0007
 * @time 18:14:43
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace server;



class AppServer
{
    private $pidfile;
    public function __construct()
    {
        $this->pidfile = __DIR__.'/../../pid/SMSGateWay.pid';
        //echo $this->pidfile;
    }

    public function run()
    {
        if (PHP_SAPI != "cli") {
            exit('please run in cli mode'.PHP_EOL);
        }
        if (!extension_loaded('inotify')) {
            exit('please install inotify extensions'.PHP_EOL);
        }

        if (!extension_loaded('swoole')) {
            exit('please install swoole extensions'.PHP_EOL);
        }

        ini_set('default_socket_timeout', -1);

        global $argc, $argv;
        if($argc < 2){
            $this->help($argv[0]);
        }else{
            if($argv[1] === 'stop'){
                $this->stop();
            }elseif($argv[1] === 'start'){
                $this->start();
            }elseif($argv[1] === 'restart'){
                $this->stop();
                $this->start();
            }elseif($argv[1] === 'reload'){
                $this->reload();
            }elseif($argv[1] === 'status'){
                $this->status();
            }else{
                $this->help($argv[0]);
            }
        }
    }

    private function help($proc){
        printf("%s start | stop | restart  | status | help \n", $proc);
    }

    private function start(){
        if(is_file($this->pidfile)){
            exit('service already started'.PHP_EOL);
        }
//        $client = new Client();
//        $client->run();
        $proc = new Process();
        $proc->run();
    }

    private function stop(){
        $proc = new Process();
        $proc->stop();
        exit;
        if(is_file($this->pidfile)){
            $pids = file_get_contents($this->pidfile);
            if($pids){
                $pids = explode(',',$pids);
                if(is_array($pids)){
                    //$pids = array_unique($pids);
                    foreach ($pids as $pid){
                        if(!$pid){
                            continue;
                        }
                        posix_kill($pid, 9);
                    }
                }

            }
            @unlink($this->pidfile);
        }else{
            exit('stop service failure'.PHP_EOL);
        }
    }

    private function reload(){
        exit('command not available'.PHP_EOL);
        if(is_file($this->pidfile)){
            $pids = file_get_contents($this->pidfile);
            if($pids){
                $pids = explode(',',$pids);
                if(is_array($pids)){
                    foreach ($pids as $pid){
                        posix_kill($pid, SIGHUP);
                    }
                }
            }
        }else{
            exit('failed to reload service'.PHP_EOL);
        }
    }

    private function status(){

        if(is_file($this->pidfile)){
            $pids = file_get_contents($this->pidfile);
            if($pids){
                $pids = explode(',',$pids);
                if(is_array($pids)){
                    foreach ($pids as $pid){
                        system(sprintf("ps ax | grep %s | grep -v grep", $pid));
                    }
                }
            }
        }else{
            exit('please start service'.PHP_EOL);
        }
    }
}